var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var lessMiddleware = require('less-middleware');
var logger = require('morgan');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var favicon = require('serve-favicon');
var session = require('express-session');
var url = require('url');
var MySQLStore = require('express-mysql-session')(session);
var routes = require('./routes/route');
var app = express();
// var FileStore = require('session-file-store')(session);
var identityKey = 'skey';
/* 日志 */
var winston = require('winston');
var uuid = require('node-uuid');
app.use(logger('dev'));

app.use(bodyParser.json());
app.use(bodyParser.text({type: '*/xml'}));
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(cookieParser());
app.use(lessMiddleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

global.moment = require('moment');
/*var options = {
    host: '47.106.252.151',
    port: 3306,
    expiration:1000,
    user: 'root',
    password: 'Abc123456!',
    database: 'develop',
    schema:{
        tableName: 'session',
        columnNames: {
            session_id: 'sessionid',
            expires: 'time',
            data: 'data'
        }
    }
};
var sessionStore = new MySQLStore(options);

app.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: sessionStore,
    resave: false,
    saveUninitialized: false,
    cookie:{
        // secure: true,
        httpOnly: true,
        // domain: 'example.com',
        // path: 'foo/bar',
        expires: 60*60*1000
    }
}));*/

routes(app);
// app.use(session({
//     name: identityKey,
//     secret: 'zhixing',  // 用来对session id相关的cookie进行签名
//     store: new FileStore(),  // 本地存储session（文本文件，也可以选择其他store，比如redis的）
//     saveUninitialized: false,  // 是否自动保存未初始化的会话，建议false
//     resave: false,  // 是否每次都重新保存会话，建议false
//     cookie: {
//         maxAge: 10 * 1000  // 有效期，单位是毫秒
//     }
// }));
// app.set('env','development');

module.exports = app;
